﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace ReceiptPrinterTest
{
    public partial class Form1 : Form
    {
        #region C++动态库 DLL Import

        /// <summary>
        ///设置参数
        /// </summary>
        /// <Param name="level">The level.</Param>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetDeviceTraceLevel")]
        public static extern void Receipt_SetDeviceTraceLevel(int level);
        /// <summary>
        /// 打开设备
        /// </summary>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_OpenDevice")]
        public static extern int Receipt_OpenDevice(StringBuilder pszRcCode);
        /// <summary>
        /// 关闭设备
        /// </summary>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_CloseDevice")]
        public static extern int Receipt_CloseDevice(StringBuilder pszRcCode);
        /// <summary>
        /// Receipt_s the set device Param.
        /// </summary>
        /// <Param name="devType">Type of the Dev.</Param>
        /// <Param name="devPort">The Dev port.</Param>
        /// <Param name="devPortParam">The Dev port Param.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetDeviceParam")]
        public static extern int Receipt_SetDeviceParam(string devType, string devPort, string devPortParam, StringBuilder pszRcCode);
        /// <summary>
        /// Receipt_s the get error MSG.
        /// </summary>
        /// <Param name="errcode">The errcode.</Param>
        /// <Param name="errmsg">The errmsg.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_GetErrorMsg")]
        public static extern int Receipt_GetErrorMsg(string errcode, string errmsg, StringBuilder pszRcCode);
        /// <summary>
        /// 初始化设备
        /// </summary>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_Reset")]
        public static extern int Receipt_Reset(int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 获取设备信息
        /// </summary>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="devid">The devid.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_GetDevid")]
        public static extern int Receipt_GetDevid(int dwTimeOut, StringBuilder devid, StringBuilder pszRcCode);
        /// <summary>
        /// 获取设备状态
        /// </summary>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="status">The status.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_GetStatus")]
        public static extern int Receipt_GetStatus(int dwTimeOut, ref int status, StringBuilder pszRcCode);
        /// <summary>
        /// 设备蜂鸣
        /// </summary>
        /// <Param name="beepType">Type of the Beep.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_Beep")]
        public static extern int Receipt_Beep(int beepType, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 打印LOGO图标
        /// </summary>
        /// <Param name="logoIndex">Index of the logo.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_PrintLogo")]
        public static extern int Receipt_PrintLogo(int logoIndex, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 打印多行数据
        /// </summary>
        /// <Param name="printData">The print data.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="printedRow">The printed row.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_PrintData")]
        public static extern int Receipt_PrintData(string printData, int dwTimeOut, ref int printedRow, StringBuilder pszRcCode);
        /// <summary>
        /// 标签打印
        /// </summary>
        /// <param name="printData"></param>
        /// <param name="dwTimeOut"></param>
        /// <param name="printedRow"></param>
        /// <param name="pszRcCode"></param>
        /// <returns></returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_TagPrint")]
        public static extern int Receipt_TagPrint(string printData, int dwTimeOut, ref int printedRow, StringBuilder pszRcCode);
        /// <summary>
        /// 打印单行数据
        /// </summary>
        /// <Param name="printData">The print data.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_PrintRowData")]
        public static extern int Receipt_PrintRowData(string printData, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 切纸
        /// </summary>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_CutPaper")]
        public static extern int Receipt_CutPaper(int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 设置中文模式
        /// </summary>
        /// <Param name="mode">The mode.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetCHNMode")]
        public static extern int Receipt_SetCHNMode(int mode, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 设置或取消下划线模式
        /// </summary>
        /// <Param name="mode">The mode.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetUnderLine")]
        public static extern int Receipt_SetUnderLine(int mode, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 最大字体
        /// </summary>
        /// <param name="mode"></param>
        /// <param name="dwTimeOut"></param>
        /// <param name="pszRcCode"></param>
        /// <returns></returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetDoubleCharMode")]
        public static extern int Receipt_SetDoubleCharMode(int mode, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 设置行间距
        /// </summary>
        /// <Param name="value">The value.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetRowDistance")]
        public static extern int Receipt_SetRowDistance(float value, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 设置左边距
        /// </summary>
        /// <Param name="value">The value.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetLeftDistance")]
        public static extern int Receipt_SetLeftDistance(float value, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 设置顶边距
        /// </summary>
        /// <Param name="value">The value.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetTopDistance")]
        public static extern int Receipt_SetTopDistance(float value, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 设置字符间距
        /// </summary>
        /// <Param name="value">The value.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetCharDistance")]
        public static extern int Receipt_SetCharDistance(float value, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 设置或取消倍高
        /// </summary>
        /// <Param name="mode">The mode.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetDoubleHeightMode")]
        public static extern int Receipt_SetDoubleHeightMode(int mode, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 设置或取消倍宽
        /// </summary>
        /// <Param name="mode">The mode.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetDoubleWidthMode")]
        public static extern int Receipt_SetDoubleWidthMode(int mode, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 设置或取消加粗
        /// </summary>
        /// <Param name="mode">The mode.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetBoldMode")]
        public static extern int Receipt_SetBoldMode(int mode, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 设置或取消黑标模式
        /// </summary>
        /// <Param name="mode">The mode.</Param>
        /// <Param name="timeOut">The time out.</Param>
        /// <Param name="reCode">The re Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetBlackMarkMode")]
        private static extern int Receipt_SetBlackMarkMode(int mode, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 功能描述：  检测黑标并走纸到黑标位置 
        /// </summary>
        /// <Param name="dwTimeOut">long dwTimeOut：超时时间，默认为 5000，单位是毫秒</Param>
        /// <Param name="pszRcCode">char *pszRcCode：失败时返回10 位错误码，成功时返回”0000000000” </Param>
        /// <returns>函数返回：  0－>成功 1－>失败 </returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_CheckBlackMark")]
        public static extern int Receipt_CheckBlackMark(int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// Receipt_s the set black mark Param.
        /// </summary>
        /// <Param name="ctbLen">The CTB len.</Param>
        /// <Param name="ctpLen">The CTP len.</Param>
        /// <Param name="pageLen">The page len.</Param>
        /// <Param name="L0Len">The l0 len.</Param>
        /// <Param name="Q0Len">The q0 len.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetBlackMarkParam")]
        public static extern int Receipt_SetBlackMarkParam(float ctbLen, float ctpLen, float pageLen, float L0Len, float Q0Len, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// Receipt_s the set page mode.
        /// </summary>
        /// <Param name="mode">The mode.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetPageMode")]
        public static extern int Receipt_SetPageMode(int mode, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// Receipt_s the set page area.
        /// </summary>
        /// <Param name="leftLen">The left len.</Param>
        /// <Param name="topLen">The top len.</Param>
        /// <Param name="width">The width.</Param>
        /// <Param name="height">The height.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetPageArea")]
        public static extern int Receipt_SetPageArea(float leftLen, float topLen, float width, float height, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// Receipt_s the set print direction.
        /// </summary>
        /// <Param name="mode">The mode.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetPrintDirection")]
        public static extern int Receipt_SetPrintDirection(int mode, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// Receipt_s the page feed.
        /// </summary>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_PageFeed")]
        public static extern int Receipt_PageFeed(int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// Receipt_s the forward row.
        /// </summary>
        /// <Param name="direction">The direction.</Param>
        /// <Param name="rowNum">The row num.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_ForwardRow")]
        public static extern int Receipt_ForwardRow(int direction, int rowNum, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// Receipt_s the forward mm.
        /// </summary>
        /// <Param name="direction">The direction.</Param>
        /// <Param name="length">The length.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_ForwardMm")]
        public static extern int Receipt_ForwardMm(int direction, float length, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 功能描述：直接发送指令到设备
        /// </summary>
        /// <Param name="command">char *command：指令数据</Param>
        /// <Param name="cmdLen">int cmdLen：指令数据长度</Param>
        /// <Param name="dwTimeOut">long dwTimeOut：超时时间，默认为 5000，单位是毫秒</Param>
        /// <Param name="pszRcCode">char *pszRcCode：失败时返回10 位错误码，成功时返回”0000000000”</Param>
        /// <returns>函数返回：  0－>成功  1－>失败</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SendCommand")]
        public static extern int Receipt_SendCommand(string command, int cmdLen, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 打印条码
        /// </summary>
        /// <Param name="printData">The print data.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_PrintBarCode")]
        public static extern int Receipt_PrintBarCode(string printData, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 打印BMP文件
        /// </summary>
        /// <Param name="szBmpFile">The sz BMP file.</Param>
        /// <Param name="offsetX">The offset X.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_PrintBMPFile")]
        public static extern int Receipt_PrintBMPFile(string szBmpFile, int offsetX, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 设置打印条码的高度
        /// </summary>
        /// <Param name="Height">The height.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetBarHeight")]
        public static extern int Receipt_SetBarHeight(int Height, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 设置打印条码的宽度
        /// </summary>
        /// <Param name="Width">The width.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetBarWidth")]
        public static extern int Receipt_SetBarWidth(int Width, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 设置打印条码的文字位置
        /// </summary>
        /// <Param name="Width">The width.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetBarHRI")]
        public static extern int Receipt_SetBarHRI(int hri, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 设置打印条码的文字对齐方式
        /// </summary>
        /// <Param name="Width">The width.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_SetBarJustification")]
        public static extern int Receipt_SetBarJustification(int just, int dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 设置打印条码的文字对齐方式
        /// </summary>
        /// <Param name="Width">The width.</Param>
        /// <Param name="dwTimeOut">The dw time out.</Param>
        /// <Param name="pszRcCode">The PSZ rc Code.</Param>
        /// <returns>System.Int32.</returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_PrintQRCode")]
        public static extern int Receipt_PrintQRCode(string printData, int dwTimeOut, StringBuilder pszRcCode);
        //Receipt_PrintGenQRCode(char *printData,int Margin,int MoudleSize, int offsetX, long dwTimeOut,char* pszRcCode);
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_PrintGenQRCode")]
        public static extern int Receipt_PrintGenQRCode(string printData, int Margin, int MoudleSize, int offsetX, Int32 dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="strText"></param>
        /// <param name="Margin"></param>
        /// <param name="ModuleSize"></param>
        /// <param name="outfile"></param>
        /// <param name="pszRcCode"></param>
        /// <returns></returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_EncodeString")]
        public static extern int Receipt_EncodeString(string strText, int Margin, int ModuleSize, string outfile, StringBuilder pszRcCode);
        /// <summary>strText
        /// 清除串口
        /// </summary>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_Fit")]
        public static extern void Receipt_Fit();

        /// <summary>
        /// 获取固件版本
        /// </summary>
        /// <param name="firmwareVersion">固件版本</param>
        /// <param name="dwTimeOut">超时时间</param>
        /// <param name="pszRcCode">返回码</param>
        /// <returns></returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_GetVersion")]
        public static extern int Receipt_GetFirmwareVersion(StringBuilder firmwareVersion, Int32 dwTimeOut, StringBuilder pszRcCode);
        /// <summary>
        /// 生成条码
        /// </summary>
        /// <param name="strText"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="outfile"></param>
        /// <param name="pszRcCode"></param>
        /// <returns></returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_BarCodeString")]
        public static extern int Receipt_BarCodeString(string strText, int width, int height, string outfile, StringBuilder pszRcCode);

        /// <summary>
        /// 获取串口指令
        /// </summary>
        /// <param name="comBuff"></param>
        /// <param name="pszRcCode"></param>
        /// <returns></returns>
        [DllImport("GWI_ReceiptPrinter_Driver.dll", EntryPoint = "Receipt_GetComBuff")]
        public static extern int Receipt_GetComBuff(StringBuilder comBuff, StringBuilder pszRcCode);
        #endregion

        #region 私有变量
        /// <summary>
        /// 设备开/关状态
        /// </summary>
        private static Int32 MAX_PATH = 260;
        /// <summary>
        /// 超时时间
        /// </summary>
        public Int32 m_dwTimeOut = 5000;
        /// <summary>
        /// 错误码
        /// </summary>
        public StringBuilder m_pszRcCode = new StringBuilder(260);
        public Int32 ret;
        #endregion


        public Form1()
        {
            InitializeComponent();
            //CHN 2016/12/13 add by wsh 修改超时时间为负数、空时，测试程序将异常退出  start
            //m_dwTimeOut = Convert.ToInt32(txtTime.Text.ToString());
            Int32.TryParse(txtTime.Text.ToString(), out m_dwTimeOut);
            //CHN 2016/12/13 add by wsh   超时时间为负数、空时，测试程序将异常退出  end
        }

        private bool UpdateTimeout()
        {
            bool tok = int.TryParse(txtTime.Text, out m_dwTimeOut);
            if (!tok)
            {
                rtxLog.AppendText("超时时间输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool BlackMarkMode(ref int mode)
        {
            bool iok = int.TryParse(txtBMmode.Text, out mode);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool CHNMode(ref int mode)
        {
            bool iok = int.TryParse(txtCHN.Text, out mode);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool UnderLine(ref int mode)
        {
            bool iok = int.TryParse(txtUnderLine.Text, out mode);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool DoubleHeightMode(ref int mode)
        {
            bool iok = int.TryParse(txtDoubleHeight.Text, out mode);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool DoubleWidthMode(ref int mode)
        {
            bool iok = int.TryParse(txtDoubleWidth.Text, out mode);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool BoldMode(ref int mode)
        {
            bool iok = int.TryParse(txtBold.Text, out mode);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool PageMode(ref int mode)
        {
            bool iok = int.TryParse(txtPageMode.Text, out mode);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool PrintDirection(ref int mode)
        {
            bool iok = int.TryParse(txtPageDirect.Text, out mode);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool ForwardRow(ref int direction, ref int rowNum)
        {
            bool iok = int.TryParse(txtRowDirection.Text, out direction);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            iok = int.TryParse(txtRow.Text, out rowNum);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool ForwardMm(ref int direction)
        {
            bool iok = int.TryParse(txtDistincDirection.Text, out direction);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool PrintBMPFile(ref int offsetX)
        {
            bool iok = int.TryParse(txtQRoffsetX.Text, out offsetX);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool BarHeight(ref int Height)
        {
            bool iok = int.TryParse(txtBCheight.Text, out Height);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool BarWidth(ref int Width)
        {
            bool iok = int.TryParse(txtBCwidth.Text, out Width);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool BarJustification(ref int Just)
        {
            bool iok = int.TryParse(txtBCjusti.Text, out Just);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool BarHRI(ref int HRI)
        {
            bool iok = int.TryParse(txtBChri.Text, out HRI);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool EncodeString(ref int Margin, ref int ModuleSize)
        {
            bool iok = int.TryParse(txtQRmargin.Text, out Margin);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            iok = int.TryParse(txtQRmoudleSize.Text, out ModuleSize);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }
        private bool BarCodeString(ref int wight, ref int height)
        {
            bool iok = int.TryParse(txtBCwidth.Text, out wight);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            iok = int.TryParse(txtBCheight.Text, out height);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private bool DoubleCharMode(ref int mode)
        {
            bool iok = int.TryParse(txtMaxFont.Text, out mode);
            if (!iok)
            {
                rtxLog.AppendText("模式参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {

            //Receipt_SetDeviceTraceLevel(4369);
            // 连接卡机
            ret = 0;
            //m_pszRcCode.Clear();
            ret = Receipt_OpenDevice(m_pszRcCode);
            LogInfo(ret, "Receipt_OpenDevice", m_pszRcCode);
        }



        private void LogInfo(int ret, string fun, StringBuilder psz)
        {
            rtxLog.AppendText("函数：" + fun.ToString() + "返回值：" + ret.ToString() + "状态码:" + psz.ToString());
            rtxLog.AppendText("\r\n");
        }

        private void LogInfo(string fun, string key, string values)
        {
            rtxLog.AppendText("函数：" + fun.ToString() + "参数：" + key.ToString() + "结果:" + values.ToString());
            rtxLog.AppendText("\r\n");
        }

        //clear 
        private void btn_Clear_Click(object sender, EventArgs e)
        {
            rtxLog.Clear();
        }

        private void btn_Log_Click(object sender, EventArgs e)
        {
            if (txtLog.TextLength <= 4)
            {
                try
                {
                    int Level = Convert.ToInt32(txtLog.Text, 16);
                    Receipt_SetDeviceTraceLevel(Level);
                    rtxLog.AppendText("设置日志等级完成。\r\n");
                }
                catch (Exception)
                {
                    rtxLog.AppendText("日志等级输入有误！无法调用接口测试。\r\n");
                }
            }
            else
            {
                rtxLog.AppendText("日志等级输入有误！无法调用接口测试。\r\n");
            }
        }

        private void btn_SetParams_Click(object sender, EventArgs e)
        {
            // 开启卡机参数
            //string strDev = txtDev.Text.ToString();
            //string strPort = txtPort.Text.ToString();
            string strParams = txtParams.Text.ToString();
            ret = Receipt_SetDeviceParam(txtType.Text, txtPort.Text, strParams, m_pszRcCode);
            LogInfo(ret, "Receipt_SetDeviceParam", m_pszRcCode);
        }

        private void btn_Fit_Click(object sender, EventArgs e)
        {
            Receipt_Fit();
            rtxLog.AppendText("清除串口成功。\r\n");
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            ret = Receipt_Reset(m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_Reset", m_pszRcCode);
        }

        private void btnGetStatus_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int status = 0;
            ret = Receipt_GetStatus(m_dwTimeOut, ref status, m_pszRcCode);
            LogInfo(ret, "Receipt_GetStatus", m_pszRcCode);
            LogInfo("Receipt_GetStatus", "status", status.ToString());
        }

        /// <summary>
        /// 设备类型
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_GetDevid_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            StringBuilder devid = new StringBuilder(MAX_PATH);
            ret = Receipt_GetDevid(m_dwTimeOut, devid, m_pszRcCode);
            LogInfo(ret, "Receipt_GetDevid", m_pszRcCode);
            LogInfo("Receipt_GetDevid", "devid", devid.ToString());
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            ret = Receipt_CloseDevice(m_pszRcCode);
            LogInfo(ret, "Receipt_CloseDevice", m_pszRcCode);
        }

        /// <summary>
        /// 下划线
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_UnderLine_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int mode = 0;
            if (UnderLine(ref mode))
            {
                return;
            }
            ret = Receipt_SetUnderLine(mode, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetUnderLine", m_pszRcCode);
        }

        private void btn_DoubleHeight_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int mode = 0;
            if (DoubleHeightMode(ref mode))
            {
                return;
            }
            ret = Receipt_SetDoubleHeightMode(mode, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetDoubleHeightMode", m_pszRcCode);
        }

        private void btn_DoubleWidth_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int mode = 0;
            if (DoubleWidthMode(ref mode))
            {
                return;
            }
            ret = Receipt_SetDoubleWidthMode(mode, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetDoubleWidthMode", m_pszRcCode);
        }

        private void btn_Bold_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int mode = 0;
            if (BoldMode(ref mode))
            {
                return;
            }
            ret = Receipt_SetBoldMode(mode, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetBoldMode", m_pszRcCode);
        }

        private void btn_CHN_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int mode = 0;
            if (CHNMode(ref mode))
            {
                return;
            }
            ret = Receipt_SetCHNMode(mode, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetCHNMode", m_pszRcCode);
        }

        private bool RowDistance(ref float mode)
        {
            bool iok = float.TryParse(txtRowDistance.Text.ToString(), out mode);
            if (!iok)
            {
                rtxLog.AppendText("参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private void btn_RowDistance_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            float value = 0;
            if (RowDistance(ref value))
            {
                return;
            }
            float.TryParse(txtRowDistance.Text.ToString(), out value);
            ret = Receipt_SetRowDistance(value, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetRowDistance", m_pszRcCode);
        }

        private bool LeftDistance(ref float mode)
        {
            bool iok = float.TryParse(txtLeftDistance.Text.ToString(), out mode);
            if (!iok)
            {
                rtxLog.AppendText("参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private void btn_LeftDistance_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            float value = 0;
            if (LeftDistance(ref value))
            {
                return;
            }
            ret = Receipt_SetLeftDistance(value, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetLeftDistance", m_pszRcCode);
        }

        private bool TopDistance(ref float mode)
        {
            bool iok = float.TryParse(txtTopDistance.Text.ToString(), out mode);
            if (!iok)
            {
                rtxLog.AppendText("参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private void btn_TopDistance_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            float value = 0;
            if(TopDistance(ref value))
            {
                return;
            }
            ret = Receipt_SetTopDistance(value, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetTopDistance", m_pszRcCode);
        }

        private bool CharDistance(ref float mode)
        {
            bool iok = float.TryParse(txtCharDistance.Text.ToString(), out mode);
            if (!iok)
            {
                rtxLog.AppendText("参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private void btn_CharDistance_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            float value = 0;
            if (CharDistance(ref value))
            {
                return;
            }
            ret = Receipt_SetCharDistance(value, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetCharDistance", m_pszRcCode);
        }

        private void btn_ForwardRow_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            } 
            int direction = 0;
            int rowNum = 0;
            if (ForwardRow(ref direction, ref rowNum))
            {
                return;
            }
            ret = Receipt_ForwardRow(direction, rowNum, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_ForwardRow", m_pszRcCode);
        }

        private void btn_ForwardMm_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int direction = 0;
            if (ForwardMm(ref direction))
            {
                return;
            }
            //Int32 direction = Convert.ToInt32(txtDistincDirection.Text.ToString());
            float distinct;
            float.TryParse(txtDistinct.Text.ToString(), out distinct);
            ret = Receipt_ForwardMm(direction, distinct, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_ForwardMm", m_pszRcCode);
        }

        private void btn_PageMode_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int mode = 0;
            if (PageMode(ref mode))
            {
                return;
            }
            ret = Receipt_SetPageMode(mode, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetPageMode", m_pszRcCode);
        }


        private bool PageArea(ref float leftLen, ref float topLen, ref float width, ref float height)
        {
            bool iok1 = float.TryParse(txtPageLeft.Text.ToString(), out leftLen);
            bool iok2 = float.TryParse(txtPageTop.Text.ToString(), out topLen);
            bool iok3 = float.TryParse(txtPageWidth.Text.ToString(), out width);
            bool iok4 = float.TryParse(txtPageHeight.Text.ToString(), out height);
            if (!iok1 || !iok2 || !iok3 || !iok4)
            {
                rtxLog.AppendText("参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private void btn_PageArea_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            float leftLen = 0, topLen = 0, width = 0, height = 0;
            if(PageArea(ref leftLen,ref topLen,ref width,ref height))
            {
                return;
            }
            ret = Receipt_SetPageArea(leftLen, topLen, width, height, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetPageArea", m_pszRcCode);
        }

        private void btn_PageDirect_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int mode = 0;
            if (PrintDirection(ref mode))
            {
                return;
            }
            ret = Receipt_SetPrintDirection(mode, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetPrintDirection", m_pszRcCode);
        }

        private void btn_PageFeed_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            ret = Receipt_PageFeed(m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_PageFeed", m_pszRcCode);
        }
        /// <summary>
        /// 黑标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_BMmode_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int mode = 0;
            if (BlackMarkMode(ref mode))
            {
                return;
            }
            ret = Receipt_SetBlackMarkMode(mode, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetBlackMarkMode", m_pszRcCode);
        }

        private bool BMparams(ref float ctbLen, ref float ctpLen, ref float pageLen, ref float L0Len, ref float Q0Len)
        {
            bool iok1 = float.TryParse(txtBMctbLen.Text.ToString(), out ctbLen);
            bool iok2 = float.TryParse(txtBMctpLen.Text.ToString(), out ctpLen);
            bool iok3 = float.TryParse(txtBMpageLen.Text.ToString(), out pageLen);
            bool iok4 = float.TryParse(txtL0Len.Text.ToString(), out L0Len);
            bool iok5 = float.TryParse(txtQ0Len.Text.ToString(), out Q0Len);
            if (!iok1 || !iok2 || !iok3 || !iok4 || !iok5)
            {
                rtxLog.AppendText("参数输入有误！无法调用接口测试。\r\n");
                return true;
            }
            return false;
        }

        private void btn_BMparams_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            float ctbLen = 0, ctpLen = 0, pageLen = 0, L0Len = 0, Q0Len = 0;
            if(BMparams(ref ctbLen,ref ctpLen,ref pageLen,ref L0Len,ref Q0Len))
            {
                return;
            }
            ret = Receipt_SetBlackMarkParam(ctbLen, ctpLen, pageLen, L0Len, Q0Len, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetBlackMarkParam", m_pszRcCode);
        }

        private void btn_BMfeed_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            ret = Receipt_CheckBlackMark(m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_CheckBlackMark", m_pszRcCode);
        }

        /// <summary>
        /// 打印条码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_BCprint_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            string data = txtBCdata.Text.ToString();
            ret = Receipt_PrintBarCode(data, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_PrintBarCode", m_pszRcCode);
        }


        private void btn_BChri_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int HRI = 0;
            if (BarHRI(ref HRI))
            {
                return;
            }
            ret = Receipt_SetBarHRI(HRI, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetBarHRI", m_pszRcCode);
        }

        private void btn_BCjustification_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int Just = 0;
            if (BarJustification(ref Just))
            {
                return;
            }
            ret = Receipt_SetBarJustification(Just, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetBarJustification", m_pszRcCode);
        }

        private void btn_BCwidth_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int Width = 0;
            if (BarWidth(ref Width))
            {
                return;
            }
            ret = Receipt_SetBarWidth(Width, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetBarWidth", m_pszRcCode);
        }

        private void btn_BCheight_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int Height = 0;
            if (BarHeight(ref Height))
            {
                return;
            }
            ret = Receipt_SetBarHeight(Height, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetBarHeight", m_pszRcCode);
        }

        /// <summary>
        /// 二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_EncodeString_Click(object sender, EventArgs e)
        {
            int Margin = 0;
            int ModuleSize = 0;
            if (EncodeString(ref Margin, ref ModuleSize))
            {
                return;
            }
            try
            {
                //Int32 Margin = Convert.ToInt32(txtQRmargin.Text.ToString());
                //Int32 ModuleSize = Convert.ToInt32(txtQRmoudleSize.Text.ToString());
                string outfile = txtChangeData.Text.ToString();
                string strText = txtQRdata.Text.ToString();
                ret = Receipt_EncodeString(strText, Margin, ModuleSize, outfile, m_pszRcCode);
                LogInfo(ret, "Receipt_EncodeString", m_pszRcCode);
            }
            catch (Exception a)
            {
                ret = 1;
                rtxLog.AppendText("函数：Receipt_EncodeString;结果:ret = 1;异常信息：" + a.Message.ToString());
                rtxLog.AppendText("\r\n");
            }
        }

        private void btn_PrintRow_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            string data = txtRowData.Text.ToString();
            ret = Receipt_PrintRowData(data, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_PrintRowData", m_pszRcCode);
        }

        private void btn_PrintMultiRow_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            string data = txtMultiData.Text.ToString();
            // string data = "www.gg.com.cn\x0d\x0awww.gwi.com.cn";
            // string data = "www.gg.com.cn\r\ndwww.gwi.com.cn";
            int printRow = 0;
            ret = Receipt_PrintData(data, m_dwTimeOut, ref printRow, m_pszRcCode);
            LogInfo(ret, "Receipt_PrintData", m_pszRcCode);
            LogInfo("Receipt_PrintData", "printRow", printRow.ToString());
        }

        private void btn_Cut_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            ret = Receipt_CutPaper(m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_CutPaper", m_pszRcCode);
        }

        /// <summary>
        /// 打印二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_PrintBMPFile_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int offsetX = 0;
            if (PrintBMPFile(ref offsetX))
            {
                return;
            }
            string szBmpFile = txtChangeData.Text.ToString();
            ret = Receipt_PrintBMPFile(szBmpFile, offsetX, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_PrintBMPFile", m_pszRcCode);

        }

        /// <summary>
        /// 直接打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_PrintQRCode_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            string printData = txtQRdata.Text.ToString();
            ret = Receipt_PrintQRCode(printData, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_PrintQRCode", m_pszRcCode);
        }
        /// <summary>
        /// 转图打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_PrintGenQRCode_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int Margin = 0;
            int ModuleSize = 0;
            if (EncodeString(ref Margin, ref ModuleSize))
            {
                return;
            }
            try
            {
                string printData = txtQRdata.Text.ToString();
                //Int32 Margin = Convert.ToInt32(txtQRmargin.Text.ToString());
                //Int32 MoudleSize = Convert.ToInt32(txtQRmoudleSize.Text.ToString());
                Int32 offsetX = Convert.ToInt32(txtQRoffsetX.Text.ToString());
                ret = Receipt_PrintGenQRCode(printData, Margin, ModuleSize, offsetX, m_dwTimeOut, m_pszRcCode);
                LogInfo(ret, "Receipt_PrintGenQRCode", m_pszRcCode);
            }
            catch (Exception a)
            {
                ret = 1;
                rtxLog.AppendText("函数：Receipt_PrintGenQRCode;结果:ret = 1;异常信息：" + a.Message.ToString());
                rtxLog.AppendText("\r\n");
            }
        }

        private void btn_MaxFont_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            int mode = 0;
            if (DoubleCharMode(ref mode))
            {
                return;
            }
            ret = Receipt_SetDoubleCharMode(mode, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_SetDoubleCharMode", m_pszRcCode);

        }

        private void txtTime_TextChanged(object sender, EventArgs e)
        {
            //CHN 2016/12/13 add by wsh 修改超时时间为负数、空时，测试程序将异常退出  start
            //m_dwTimeOut = Convert.ToInt32(txtTime.Text.ToString());
            Int32.TryParse(txtTime.Text.ToString(), out m_dwTimeOut);
            //CHN 2016/12/13 add by wsh   超时时间为负数、空时，测试程序将异常退出  end
        }

        /// <summary>
        /// 标签打印
        /// Receipt_TagPrint
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_TagPrint_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            StringBuilder data = new StringBuilder(204800);
            data.Append(txtMultiData.Text.ToString());
            int printRow = 0;
            ret = Receipt_TagPrint(data.ToString(), m_dwTimeOut, ref printRow, m_pszRcCode);
            LogInfo(ret, "Receipt_TagPrint", m_pszRcCode);
            LogInfo("Receipt_TagPrint", "printRow", printRow.ToString());
        }


        private void txtBCdata_TextChanged(object sender, EventArgs e)
        {
            string barcodeStr = txtBCdata.Text.ToString();
            bool digit = false, letter = false;
            int len = barcodeStr.Length;
            int i = 0;
            int charnum = len;
            for (; i < len; i++)
            {
                if (!char.IsDigit(barcodeStr[i]))
                {
                    if (letter == false)
                    {

                        letter = true;
                        digit = false;
                        charnum += 2;
                    }
                    //charnum += 1;
                }
                else
                {
                    if (len > i + 1 && char.IsDigit(barcodeStr[i + 1]))//打印内容为连续2个数字			
                    {
                        if (digit == false)
                        {
                            digit = true;
                            letter = false;
                            charnum += 2;
                        }
                        charnum -= 1;
                        i++;
                    }
                    else
                    {
                        if (letter == false)
                        {
                            letter = true;
                            digit = false;
                            charnum += 2;
                        }
                    }
                }
            }
            //
            if (0 == len)
            {
                charnum = 0;
            }
            else
            {
                charnum -= 2;
            }
            lbl_barcode.Text = charnum.ToString();
        }

        private void btnGetFirmwareVersion_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            StringBuilder firmwareVersion = new StringBuilder(260);
            ret = Receipt_GetFirmwareVersion(firmwareVersion, m_dwTimeOut, m_pszRcCode);
            LogInfo(ret, "Receipt_GetVersion", m_pszRcCode);
            LogInfo("Receipt_GetVersion", "version", firmwareVersion.ToString());
        }

        private void btn_BarCodeString_Click(object sender, EventArgs e)
        {
            int width = 0;
            int height = 0;
            if (BarCodeString(ref width, ref height))
            {
                return;
            }
            try
            {
                string outfile = txtBarData.Text.ToString();
                string strText = txtBCdata.Text.ToString();
                ret = Receipt_BarCodeString(strText, width, height, outfile, m_pszRcCode);
                LogInfo(ret, "Receipt_BarCodeString", m_pszRcCode);
            }
            catch (Exception a)
            {
                ret = 1;
                rtxLog.AppendText("函数：Receipt_BarCodeString;结果:ret = 1;异常信息：" + a.Message.ToString());
                rtxLog.AppendText("\r\n");
            }
        }

        private void btnGetComBuff_Click(object sender, EventArgs e)
        {
            if (UpdateTimeout())
            {
                return;
            }
            StringBuilder comBuff = new StringBuilder(1024);
            ret = Receipt_GetComBuff(comBuff, m_pszRcCode);
            LogInfo(ret, "Receipt_GetComBuff", m_pszRcCode);
            LogInfo("Receipt_GetComBuff", "comBuff", comBuff.ToString());
        }

        private void bt_SetInstallCut_Click(object sender, EventArgs e)
        {
            string strParams = txtParams.Text.ToString();
            ret = Receipt_SetDeviceParam(txtType.Text, txtPort.Text, strParams, m_pszRcCode);
            if (ret != 0)
            {
                LogInfo(ret, "Receipt_SetDeviceParam", m_pszRcCode);
                return;
            }

            ret = Receipt_OpenDevice(m_pszRcCode);
            if (ret != 0)
            {
                LogInfo(ret, "Receipt_OpenDevice", m_pszRcCode);
                return;
            }

            string command = "1B734245929A010046";
            int cmdLen = 9;

            ret = Receipt_SendCommand(command, cmdLen, m_dwTimeOut, m_pszRcCode);
            if (ret == 0)
            {
                MessageBox.Show("设置上纸后第一张切纸成功！", "设置提示");
            }
            else
            {
                LogInfo(ret, "Receipt_SendCommand", m_pszRcCode);
            }

            Receipt_CloseDevice(m_pszRcCode);

        }

        private void bt_CancelInstallCut_Click(object sender, EventArgs e)
        {
            string strParams = txtParams.Text.ToString();
            ret = Receipt_SetDeviceParam(txtType.Text, txtPort.Text, strParams, m_pszRcCode);
            if (ret != 0)
            {
                LogInfo(ret, "Receipt_SetDeviceParam", m_pszRcCode);
                return;
            }

            ret = Receipt_OpenDevice(m_pszRcCode);
            if (ret != 0)
            {
                LogInfo(ret, "Receipt_OpenDevice", m_pszRcCode);
                return;
            }

            string command = "1B734245929A000046";
            int cmdLen = 9;

            ret = Receipt_SendCommand(command, cmdLen, m_dwTimeOut, m_pszRcCode);
            if (ret == 0)
            {
                MessageBox.Show("取消上纸后第一张切纸成功！", "设置提示");
            }
            else
            {
                LogInfo(ret, "Receipt_SendCommand", m_pszRcCode);
            }

            Receipt_CloseDevice(m_pszRcCode);
        }

    }
}
